import instacne from "../request";
import type { Ref } from "vue";

/**
 * 登录返回数据
 */
interface IAdminLoginParams {
  password: string;
  username: string;
}
interface IAdminLoginRes {
  tokenHead: string;
  token: string;
}

const adminLogin = (data: IAdminLoginParams): Promise<IAdminLoginRes> => {
  // instacne.post("/auth/login", data); 是一个 Promise 类型
  return instacne.post("/admin/login", data);
};

/**
 * 获取当前登录用户信息
 */
interface IAdminInfoRes {
  menus: IMneuItem[];
  icon: string;
  username: string;
  roles: string[];
}
const getAdminInfo = (): Promise<IAdminInfoRes> => {
  return instacne.get("/admin/info");
};

/**
 * /admin/list 根据用户名或者姓名获取用户列表
 */
interface IAdminListParams {
  keyword?: string | Ref<string>;
  pageNum?: number | Ref<number>;
  pageSize?: number | Ref<number>;
  id?: number | Ref<number>;
}

interface IAdminListItem {
  createTime: string;
  email: string;
  icon: string;
  id: number;
  loginTime: string;
  nickName: string;
  note: string;
  password: string;
  status: number;
  username: string;
}

interface IAdminListRes {
  list: IAdminListItem[];
  pageNum: number;
  pageSize: number;
  total: number;
  totalPage: number;
}

const getAdminList = (data?: IAdminListParams): Promise<IAdminListRes> => {
  return instacne.get("/admin/list", {
    params: data,
  });
};

/**
 * 新增用户
 * admin/register
 */
const adminRegister = (data: IAdminListItem): Promise<IAdminListItem> => {
  return instacne.post("/admin/register", data);
};

/**
 * /admin/update/{id}
 *  修改指定用户信息
 */
const adminUpdate = (data: IAdminListItem) => {
  return instacne.post(`/admin/update/${data.id}`, data)
}
/* 
     resource  /resource/list?pageNum=1&pageSize=10
*/
const getAdmineresource = (data?: IAdminListParams): Promise<IAdminListRes> => {
  return instacne.get("/resource/list", {
    params: data,
  });
};
/* 
    /menu/list/  菜单列表  
*/
const getAdminmenu = (data?: IAdminListParams): Promise<IAdminListRes> => {
  console.log("data", data);

  return instacne.get(`/menu/list/` + (data?.id ? data?.id : 0), {
    params: {
      pageNum: data?.pageNum,
      pageSize: data?.pageSize
    }
  });
};
/* 
  /menu/97
*/
interface datamenu {
  createTime: string
  hidden: number
  icon: string
  id: number
  level: number
  name: string
  parentId: number
  sort: number
  title: string
}
interface adminaddmenu {
  data: datamenu[]
}
const getAdminID = (id: number): Promise<adminaddmenu> => {
  return instacne.get(`/menu/${id}`);
};


interface IgoodsList {
  pageNum?: number | Ref<number>
  pageSize?: number | Ref<number>
  id?: number | Ref<number>
}
const getAdminproductCategory = (data?: IgoodsList): Promise<IAdminListRes> => {
  // console.log(Number(data?.id));

  return instacne.get("/productCategory/list/" + (data?.id ? data?.id : 0), {
    params: {
      pageNum: data?.pageNum,
      pageSize: data?.pageSize
    }
  });
};

/* 
  menu/create 添加权限
*/
interface buteIdList {

}
interface adminCreateItem {
  hidden?: number,
  icon?: string,
  name?: string,
  parentId?: number,
  sort?: string | number,
  title?: string

  region?: string
  description?: string
  keywords?: string
  navStatus?: number
  productAttributeIdList?: number[]
  productUnit?: string
  showStatus?: number
}
const adminCreate = (data: adminCreateItem): Promise<adminCreateItem> => {
  return instacne.post("/menu/create", data);
};

/*  
     /productCategory/create
*/
const adminCategoryCreate = (data: adminCreateItem): Promise<adminCreateItem> => {
  return instacne.post("/productCategory/create", data);
}
/* 
   /productAttribute/attrInfo/${id} 编辑商品分类
*/
const getAdminattrInfo = (id: any) => {
  return instacne.get(`/productCategory/${id}`);
};
/* 
  /productCategory/delete/148 删除
*/
const adminproductCategorydelete = (id: number) => {
  return instacne.post(`/productCategory/delete/${id}`);
};

/* 
    /menu/updateHidden/155?hidden=0
*/
interface datahidden {
  id?: number,
  hidden?: number
}
const adminupdateHidden = (data?: datahidden): Promise<datahidden> => {
  return instacne.post(`/menu/updateHidden/${data?.id}?hidden=${data?.hidden}`);
};




export { adminLogin, getAdminInfo, getAdminList, adminRegister, adminUpdate, getAdmineresource, getAdminmenu, getAdminproductCategory, adminCreate, getAdminattrInfo, getAdminID, adminproductCategorydelete, adminCategoryCreate, adminupdateHidden };
export type { IAdminLoginParams, IAdminLoginRes, IAdminListItem, adminCreateItem };
